import { TODO_ADD_TODO } from './constant'

// redux action in here
export const addTodo = (text) => (
  {
    type: TODO_ADD_TODO,
    text
  }
)

// redux reducer in here too
// array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

export const reducer = (state, action) => {
  switch (action.type) {
    case TODO_ADD_TODO:
      return {
        ...state,
        todos: [
          ...state.todos,
          {
            id: state.todos.reduce((maxId, todo) => Math.max(todo.id, maxId), 0) + 1,
            completed: false,
            text: action.text
          }
        ]
      };
      
    default:
      return state
  }
}